<?php
/**
 * Created by PhpStorm.
 * User: roy
 * Date: 2017/5/18
 * Time: 11:30
 */

namespace Fn;


class Locker
{
    static function wait($key,$maxLockTime,$callback=false){

        while (self::check($key)){
            usleep(1000000);//10000000/10 0.1s
        }
        self::_set($key,time()+$maxLockTime);

        if(is_callable($callback)){
            try {
                $r = call_user_func($callback);
                self::_set($key,0);
                return $r;
            }catch (Exception $e){
                self::_set($key,0);
                return null;
            }
        }

        return true;
    }

    static function check($key){
		return (self::_get()<time()) 
    }

    static function remove($key){
        self::_set($key,0);
    }

    private static function _set($key){

    }
	private static function _get($key){

    }
	
}